]> git.r.bdr.sh - rbdr/map/blame_incremental - Map/Presentation/Base Components/MapRender/MapEdges.swift
Update contributing
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapEdges.swift
... / ...
CommitLineData
1/*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
16 */
17import SwiftUI
18
19struct MapEdges: View {
20
21 let mapSize: CGSize
22 let lineWidth: CGFloat
23 let vertexSize: CGSize
24 let edges: [MapEdge]
25
26 let arrowheadSize = CGFloat(10.0)
27
28 var body: some View {
29 ForEach(edges, id: \.id) { edge in
30 Path { path in
31
32 // First we transform edges from percentage to map coordinates
33 let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
34 let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
35
36 let slope = (destination.y - origin.y) / (destination.x - origin.x)
37 let angle = atan(slope)
38 let multiplier = CGFloat(slope < 0 ? -1.0 : 1.0)
39 let upperAngle = angle - CGFloat.pi / 4.0
40 let lowerAngle = angle + CGFloat.pi / 4.0
41
42 let offsetOrigin = CGPoint(
43 x: origin.x + multiplier * (vertexSize.width / 2.0) * cos(angle),
44 y: origin.y + multiplier * (vertexSize.height / 2.0) * sin(angle))
45 let offsetDestination = CGPoint(
46 x: destination.x - multiplier * (vertexSize.width / 2.0) * cos(angle),
47 y: destination.y - multiplier * (vertexSize.height / 2.0) * sin(angle))
48
49 path.move(to: offsetOrigin)
50 path.addLine(to: offsetDestination)
51
52 if edge.arrowhead {
53 path.move(to: offsetDestination)
54 path.addLine(
55 to: CGPoint(
56 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
57 y:
58 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
59
60 path.move(to: offsetDestination)
61 path.addLine(
62 to: CGPoint(
63 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
64 y:
65 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
66 }
67 path.move(to: offsetDestination)
68 path.closeSubpath()
69 }.applying(
70 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
71 ).stroke(Color.map.vertexColor, lineWidth: lineWidth)
72 }
73 }
74
75 func h(_ dimension: CGFloat) -> CGFloat {
76 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
77 }
78
79 func w(_ dimension: CGFloat) -> CGFloat {
80 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
81 }
82}
83
84#Preview {
85 MapEdges(
86 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
87 vertexSize: CGSize(width: 25.0, height: 25.0),
88 edges: [
89 MapEdge(
90 id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2),
91 arrowhead: true)
92 ])
93}